package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.sqlparam.RechargeablecardsExamplePage;
import com.base.po.sqlparam.UsersExamplePage;
import com.base.po.voip.Rechargeablecards;
import com.base.po.voip.RechargeablecardsExample.Criteria;
import com.base.po.voip.Users;
import com.base.po.voip.UsersExample;
import com.base.service.LogsService;
import com.base.service.RechargeablecardsService;
import com.base.service.UsersService;
import com.base.utils.IP;

@Controller
@RequestMapping("/admin")
public class content_huishou {
	@Autowired
	private RechargeablecardsService rechargeablecardsService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private LogsService logsService;// log日志

	// 充值卡管理
	@RequestMapping("/content_huishou")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		RechargeablecardsExamplePage examplePage = new RechargeablecardsExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CAST(SUBSTRING(rechargeablecards.`CardNo`,1,7) AS SIGNED)");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Rechargeablecards> rechargeablecards = null;
		List<Users> list = null;
		Criteria criteria = examplePage.createCriteria();
		UsersExample userExample = new UsersExamplePage();
		switch (adminUsergrade) {
		case 0:
			userExample.getOredCriteria().add(userExample.createCriteria().andDatastatusEqualTo(true));
			list = usersService.selectByExample(userExample);
			break;

		case 1:
			// 查询OEM下的商户以及OEM
			userExample.getOredCriteria()
					.add(userExample.createCriteria().andParentidEqualTo(adminUserId).andDatastatusEqualTo(true));
			list = usersService.selectByExample(userExample);
			for (Users users : list) {
				examplePage.or(examplePage.createCriteria().andParentidEqualTo(users.getId()));
			}
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		case 2:
			userExample.getOredCriteria()
					.add(userExample.createCriteria().andParentidEqualTo(adminUserId).andDatastatusEqualTo(true));
			list = usersService.selectByExample(userExample);
			// 查询商家下的ID
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;
		}
		rechargeablecards = rechargeablecardsService.selectByExamplePage(examplePage);
		/*
		 * 设置分页
		 */
		int totlenum = rechargeablecardsService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", 1);// 传入session中
		request.setAttribute("users", list);// 传入session中
		if (rechargeablecards != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huishou");
			request.setAttribute("list", rechargeablecards);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "批量修改充值卡信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}